package com.faner.controller;

import com.faner.bean.ResultUtil;
import com.faner.function.Configer;
import com.faner.model.UserDomain;
import com.faner.service.user.UserService;
import com.fun.frame.SourceCode;
import com.fun.utils.RString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private Configer configer;

    @GetMapping("/get/{id}")
    @ResponseBody
    public ResultUtil get(@PathVariable(value = "id", required = true) int id, @RequestParam(name = "s", required = false, defaultValue = "1") int s) {
        if (s > 1) id = 33;
        UserDomain user = userService.findUser(id);
        addUser();
        return ResultUtil.build(user);
    }

    @RequestMapping(value = "/ts")
    @ResponseBody
    public Configer ss() {
        return configer;
    }

    @ResponseBody
    @GetMapping("/add")
    public ResultUtil addUser() {
        UserDomain user = new UserDomain();
        user.setPhone(SourceCode.getRandomIP());
        user.setPassword(RString.getString(10));
        user.setUserName(RString.getString(6));
        userService.addUser(user);
        logger.warn("用户：{}", user);
        return ResultUtil.success(user);
    }

    @ResponseBody
    @GetMapping("/all")
    public Object findAllUser(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize) {
        return userService.findAllUser(pageNum, pageSize);
    }

}
